package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.neuiotms.dcam.fixed.converter.AssetConverter;
import com.neusoft.neuiotms.dcam.fixed.converter.InventoryResultTemporaryConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryDutyDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryResultTemporaryDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Asset;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.InventoryResultTemporaryRepository;
import com.neusoft.neuiotms.dcam.fixed.service.InventoryResultTemporaryService;

@Service
@Transactional
public class InventoryResultTemporaryServiceImpl implements InventoryResultTemporaryService {

    @Autowired
    private InventoryResultTemporaryRepository inventoryResultTemporaryRepository;
    @Autowired
    private InventoryResultTemporaryConverter inventoryResultTemporaryConverter;
    @Autowired
    private AssetRepository assetRepository;
    @Autowired
    private AssetConverter assetConverter;

    @Override
    public List<Map<String, Object>> list(final Long id, final UserBean userBean) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (id != null) {
            list = this.inventoryResultTemporaryRepository.list(id);
        } else {
            //            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            final Date end = new Date();
            System.out.print("当前：");
            //            System.out.println(sdf.format(end));
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(end);
            calendar.add(Calendar.MINUTE, -5);
            final Date start = calendar.getTime();
            System.out.print("五分钟前：");
            //            System.out.println(sdf.format(start));
            list = this.inventoryResultTemporaryRepository.listByDate(start, end, userBean.getUsername());
        }
        return list;
    }

    @Override
    public InventoryDutyDTO create(final InventoryDutyDTO dto, final UserBean userBean) {
        if ((dto.getId() == null) || (dto.getId() == -1)) {
            final Date end = new Date();
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(end);
            calendar.add(Calendar.MINUTE, -60);
            final Date start = calendar.getTime();
            this.inventoryResultTemporaryRepository.deleteByInventoryDate(start, end, userBean.getUsername());
        } else {
            this.inventoryResultTemporaryRepository.deleteByInventoryDutyId(dto.getId());
        }
        //        this.inventoryResultTemporaryRepository.deleteByInventoryDuty_Id(dto.getId());
        final List<InventoryResultTemporaryDTO> dutyResultsTem = dto.getDutyResultsTem();
        final List<String> idList = new ArrayList<>();
        for (final InventoryResultTemporaryDTO results : dutyResultsTem) {
            idList.add(results.getCode());
        }
        final List<Asset> assets = this.assetRepository.findByCodeIn(idList);
        final List<AssetDTO> dtoArr = this.assetConverter.toListDTO(assets);
        for (final AssetDTO k : dtoArr) {
            final InventoryResultTemporaryDTO r =
                    this.inventoryResultTemporaryConverter.toDTO(k, dto.getId(), userBean.getUsername());
            // 下发资产
            this.inventoryResultTemporaryRepository.save(this.inventoryResultTemporaryConverter.toModel(r));
        }
        return dto;
    }

    @Override
    public void delete(final Long id, final UserBean userBean) {
        if ((id == null) || (id == -1)) {
            final Date end = new Date();
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(end);
            calendar.add(Calendar.MINUTE, -5);
            final Date start = calendar.getTime();
            this.inventoryResultTemporaryRepository.deleteByInventoryDate(start, end, userBean.getUsername());
        } else {
            this.inventoryResultTemporaryRepository.deleteByInventoryDutyId(id);
        }
    }

}
